# -*- coding: utf-8 -*-
# File : contract.py
# Author: taoyahui
# Date : 2021/7/20
import json
from HelloWorld import utils, models
import results
from django.http import HttpResponse


def set_contract(request):
    res_json = json.loads(request.body.decode().replace("'", "\""))
    if not utils.has_keys(res_json, 'name', 'address'):
        return HttpResponse(json.dumps(results.info_incomplete_result))

    name = res_json['name']
    address = res_json['address']
    abi = res_json['abi']

    contract = models.Contract(name=name, address=address, abi=abi)
    contract.save()
    return HttpResponse(json.dumps(results.success_result))


def get_contract(request):
    name = request.GET.get('name')

    contract = models.Contract.objects.get(name=name)
    data = {
        "name": contract.name,
        "address": contract.address
    }
    r_dict = {
        'code' : 200,
        'data': data
    }
    return HttpResponse(json.dumps(r_dict))




